package uc;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import cn.onekit.android.ACTIVITY;
import co.chuangdian.oflauncher.R;

@SuppressWarnings("deprecation")
public class BannerView extends LinearLayout {

	public BannerView(Context context) {
		super(context);
		init();
	}

	public BannerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	Bitmap[] images = new Bitmap[] {};
	String[] titles = new String[] {};
	Gallery bannersView;
	RadioGroup pageControl;
	BaseAdapter adapter;

	void init() {
		View.inflate(getContext(), R.layout.uc_bannerview, this);
		bannersView = (Gallery) this.findViewById(R.id.bannersView);
		bannersView.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				((RadioButton) pageControl.getChildAt(arg2)).setChecked(true);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				pageControl.clearCheck();
			}

		});
		pageControl = (RadioGroup) this.findViewById(R.id.pageControl);
		adapter = new BaseAdapter() {

			@Override
			public int getCount() {
				return images.length;
			}

			@Override
			public Object getItem(int position) {
				return images[position];
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				LinearLayout viewGroup = new LinearLayout(getContext());
				viewGroup.setOrientation(LinearLayout.VERTICAL);
				//
				TextView textView = new TextView(getContext());
				LinearLayout.LayoutParams textViewLayoutParams = new LinearLayout.LayoutParams(
						LayoutParams.MATCH_PARENT, ACTIVITY.dp2px(50));
				textViewLayoutParams.weight = 0;
				textView.setLayoutParams(textViewLayoutParams);
				textView.setGravity(Gravity.CENTER);
				textView.setTextColor(Color.BLACK);
				textView.setTextSize(25);
				textView.setText(titles[position]);
				viewGroup.addView(textView);
				//
				ImageView imageView = new ImageView(getContext());
				LinearLayout.LayoutParams imageViewLayoutParams = new LinearLayout.LayoutParams(
						LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
				imageViewLayoutParams.weight = 1;
				imageView.setLayoutParams(imageViewLayoutParams);
				imageView.setScaleType(ScaleType.CENTER_CROP);
				imageView.setImageBitmap(images[position]);
				viewGroup.addView(imageView);
				//
				return viewGroup;
			}

		};
		bannersView.setAdapter(adapter);
	}

	public void setData(final String[] titles, final Bitmap[] images) {
		this.titles = titles;
		this.images = images;
		//
		pageControl.removeAllViews();
		for (int i = 0; i < titles.length; i++) {
			pageControl.addView(new RadioButton(getContext()));
		}
		//
		adapter.notifyDataSetChanged();
	}
}
